package cmd

import (
	"context"
	"gf-xb-api/cwt/mymiddleware"
	"gf-xb-api/internal/consts"
	"gf-xb-api/internal/controller/admin"
	"gf-xb-api/internal/controller/captcha"
	"gf-xb-api/internal/controller/membership"
	"gf-xb-api/internal/controller/role"
	"gf-xb-api/internal/controller/role_api"
	"gf-xb-api/internal/controller/user"
	"gf-xb-api/internal/controller/user_role"
	"gf-xb-api/internal/controller/vapi"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
)

var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			s := g.Server(g.Cfg().MustGet(ctx, "server.name").String())

			// 绑定 /assets 路径到项目中的 assets 目录
			s.AddStaticPath("/assets", "/assets")

			s.BindMiddlewareDefault(mymiddleware.Middleware().CORS)

			s.Group("/api", func(group *ghttp.RouterGroup) {
				// group.Middleware(ghttp.MiddlewareHandlerResponse)
				group.Middleware(mymiddleware.MiddlewareHandlerResponse, mymiddleware.Middleware().Auth)
				group.Bind(
					admin.NewV1(),
					membership.NewV1(),
					captcha.NewV1(),
					user.NewV1(),
					user_role.NewV1(),
					vapi.NewV1(),
					role.NewV1(),
					role_api.NewV1(),
				)
			})

			s.SetSwaggerUITemplate(consts.MySwaggerUITemplate)
			s.Run()
			return nil
		},
	}
)
